$( document ).ready(function() {

	var savedImages = new Array();
	var savedPrices = new Array();
	var savedRetailers = new Array();
	var savedLinks = new Array();


	$(".hideatstart").hide();

  	$("#welcomepumpkin").bind( "click", function() {
		$("#titlebar").show("slow");
		$("#welcomepage").hide("slow");
		$("#Search").show("slow");
	});
	
	function hideAllPages(){
		$(".normalpage").hide("slow");
	}
	
	var current_query;
	
	$("#searchtext").focus(function() {
		if( $(this).val() == "Pirate Hat" ) {
			$("#searchtext").val("");
		}
	});
	
	$('#searchtext').keyup(function(){
		var s = $('#searchtext').val();
		if(s.indexOf("\n") > -1){
			current_query=$("#searchtext").val();
			doSearch();
		}
	});

	$("#submitsearch").bind( "click", function() {
		current_query=$("#searchtext").val();
		doSearch();
	});
	
	var total=0;
	$(".cartButton").bind( "click", function() {
        hideAllPages();
        
        var myTable= "";
        $(".chicken").remove();
        
        total=0;
		
		for (var i=0; i<savedImages.length; i++) {	
			myTable= "";
			
			myTable+='<tr class="tablerow chicken" align="center">'
			myTable+='<td style="width: 200px;"><img src="';
			myTable+=savedImages[i];
			myTable+='" style="height:100px;width:100px"></td>';
			myTable+='<td style="width: 250px;">$';
			myTable+=savedPrices[i];
			myTable+=" from ";
			myTable+=savedRetailers[i];
			myTable+='</td><td style="width: 200px;"><img src="minusButton.png" numval="' + savedPrices[i];
			myTable+='" class="minusButton" numby="';
			myTable+=i;
			myTable+='" style="height:50px;width:50px"></td>';
			myTable+='</tr>';
			$("#cartTable").append(myTable);
			
			total+=savedPrices[i];
			console.log(myTable);
		}
		
		$("#totalcost").text("Total: $"+total);
		
       $("#Cart").show("slow");
    });
    
    $(".searchButton").bind( "click", function() {
        hideAllPages();
        $('#searchtext').val("Pirate Hat");
        $("#Search").show("slow");
    });
	
	var woot;
	
	$(document).on( "click", ".addButton", function() {
		console.log("plus clicked");
		
		var index = parseInt($(this).attr("numby"));
		
		if(typeof woot != 'undefined'){ 
			if(woot[index].hasOwnProperty('best_page')){
				if(woot[index].best_page.hasOwnProperty('image_url')){
					savedImages[savedImages.length]=woot[index].best_page.image_url;
					}else{savedImages[savedImages.length]="";}
				if(woot[index].best_page.hasOwnProperty('price')){
					savedPrices[savedPrices.length]=woot[index].best_page.price;
					} else{ savedPrices[savedPrices.length]=0;}
				if(woot[index].best_page.hasOwnProperty('retailer_name')){
					savedRetailers[savedRetailers.length]=woot[index].best_page.retailer_name;
					} else{ savedPrices[savedRetailers.length]="unknown";}
			}
		}
	});
	
	$(document).on( "click", ".minusButton", function() {
		var val=$(this).attr("numval");
		console.log("Val: "+val);
		total=total-val;
		total.toFixed(2);
		$("#totalcost").text("Total: $"+total);
		
		//console.log($(this).parent().parent());
		savedPrices.splice($(this).attr("numby"));
		savedImages.splice($(this).attr("numby"));
		savedRetailers.splice($(this).attr("numby"));
		$(this).parent().parent().remove();
	});
	

	
	function doSearch(){
	
		current_query=current_query.replace(/ /g,'+');
		current_query=current_query.replace(/:/g,'%3A');
		console.log(current_query);
	
		hideAllPages();
		$("#SearchResults").show("slow");
		
		var query_url="http://us.api.invisiblehand.co.uk/v1/products?query=";
		query_url+=current_query;
		query_url+="&include_ebay_pages=false&app_id=0f2304ae&app_key=de35d65e4e63894af19fa2ab845d97e1";
		
		console.log(current_query);
		console.log(query_url);
		$.ajax({
		  method: "GET",
		  url: query_url,
		  success: function(data, textStatus, jqXHR) {
		  
		  		$(".chicken").remove();
		  		
		  		woot=data.results;
		  		
		  		var myTable= "";
		
		    	for (var i=0; i<woot.length; i++) {	
		    		myTable= "";
		    		myTable+='<tr class="tablerow chicken" align="center">'
					myTable+='<td style="width: 200px;"><img src="';
					myTable+=woot[i].image_url;
					myTable+='" style="height:100px;width:100px"></td>';
					myTable+='<td style="width: 250px;">$';
					if(woot[i].best_page.hasOwnProperty('price')){
						myTable+=woot[i].best_page.price;
					}
					else{
						myTable+="unknown";
					}
					myTable+=" from ";
					if(woot[i].best_page.hasOwnProperty('retailer_name')){
						myTable+=woot[i].best_page.retailer_name;
					}
					else{
						myTable+="unknown";
					}
					myTable+='</td><td style="width: 200px;" class="addButton" numby="';
					myTable+=i;
					myTable+='"><img src="plusButton.png" style="height:50px;width:50px"></td>';
					myTable+='</tr>';
					myTable+='<tr style=""></tr>';
					$("#resultsTable").append(myTable);
				}  
		  }
		});
	}
	
	
});